package com.zhou.framework.filter;


import com.zhou.org.dao.BlackList;
import com.zhou.framework.model.RS;
import com.zhou.framework.util.BlackListUtil;
import com.zhou.util.TimeUtil;
import com.zhou.util.WebUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class BlackListFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig){

    }

    @Override
    public void doFilter(ServletRequest r, ServletResponse rp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) r;
        HttpServletResponse response = (HttpServletResponse) rp;

        String ip = WebUtil.getIp(request);
        BlackList item = BlackListUtil.getBlackList(ip);
        if(BlackListUtil.inBlackList(item)){
            RS result = new RS();
            result.putData("start", TimeUtil.formatDate(item.getCreateDt(),"yyyy-MM-dd HH:mm:ss"));
            result.putData("end", item.getReleaseDt() != null ? TimeUtil.formatDate(item.getReleaseDt(),"yyyy-MM-dd HH:mm:ss") : "永久");
            result.putData("reason",item.getReason());
            WebUtil.writeObject(response, result);
            return;
        }
        chain.doFilter(r,response);
    }

    @Override
    public void destroy() {

    }
}
